﻿using System;
using System.Drawing;
using System.Text;




namespace Pixysoft.RTF
{


    partial class RtfBuilder
    {
        #region Nested type: RTFFormatWrap

        /// <summary>
        /// Wraps RTFBuilderbase for formatting changes allowing injection of appropriate rtf codes to revert format after each Append (string) call
        /// </summary>
        private class RTFFormatWrap : IDisposable
        {
            #region Fields

            private readonly RtfBuilder _builder;

            #endregion

            #region Constructor

            public RTFFormatWrap(RtfBuilder builder)
            {
                this._builder = builder;
                if (this._builder._unwrapped)
                {
                    return;
                }

                StringBuilder sb = this._builder._sb;

                int len = this._builder._sb.Length;

                if (this._builder._sf.Alignment == StringAlignment.Center)
                {
                    sb.Append("\\qc");
                }
                else if (this._builder._sf.Alignment == StringAlignment.Far)
                {
                    sb.Append("\\qr");
                }
                if ((this._builder.FontTable.FontStyle & System.Drawing.FontStyle.Bold) == System.Drawing.FontStyle.Bold)
                {
                    sb.Append("\\b");
                }
                if ((this._builder.FontTable.FontStyle & System.Drawing.FontStyle.Italic) == System.Drawing.FontStyle.Italic)
                {
                    sb.Append("\\i");
                }
                if ((this._builder.FontTable.FontStyle & System.Drawing.FontStyle.Underline) == System.Drawing.FontStyle.Underline)
                {
                    sb.Append("\\ul");
                }
                if ((this._builder.FontTable.FontStyle & System.Drawing.FontStyle.Strikeout) == System.Drawing.FontStyle.Strikeout)
                {
                    sb.Append("\\strike");
                }

                if (this._builder.FontTable.FontSize != this._builder.FontTable.DefaultFontSize)
                {
                    sb.AppendFormat("\\fs{0}", this._builder.FontTable.FontSize);
                }
                if (this._builder.FontTable.Font != 0)
                {
                    sb.AppendFormat("\\f{0}", this._builder.FontTable.Font);
                }
                if (this._builder.ColorTable.ForeColor != this._builder.ColorTable.DefaultForeColor)
                {
                    sb.AppendFormat("\\cf{0}", this._builder.ColorTable.IndexOf(this._builder.ColorTable.ForeColor));
                }
                if (this._builder.ColorTable.BackColor != this._builder.ColorTable.DefaultBackColor)
                {
                    sb.AppendFormat("\\highlight{0}", this._builder.ColorTable.IndexOf(this._builder.ColorTable.BackColor));
                }


                if (sb.Length > len)
                {
                    sb.Append(" ");
                }
            }

            #endregion

            #region Override Methods

            ~RTFFormatWrap()
            {
                this.Dispose(false);
            }

            #endregion

            #region Methods

            protected void Dispose(bool disposing)
            {
                if (this._builder != null && !this._builder._unwrapped)
                {

                    StringBuilder sb = this._builder._sb;

                    int len = sb.Length;
                    if ((this._builder.FontTable.FontStyle & System.Drawing.FontStyle.Bold) == System.Drawing.FontStyle.Bold)
                    {
                        sb.Append("\\b0");
                    }
                    if ((this._builder.FontTable.FontStyle & System.Drawing.FontStyle.Italic) == System.Drawing.FontStyle.Italic)
                    {
                        sb.Append("\\i0");
                    }
                    if ((this._builder.FontTable.FontStyle & System.Drawing.FontStyle.Underline) == System.Drawing.FontStyle.Underline)
                    {
                        sb.Append("\\ulnone");
                    }
                    if ((this._builder.FontTable.FontStyle & System.Drawing.FontStyle.Strikeout) == System.Drawing.FontStyle.Strikeout)
                    {
                        sb.Append("\\strike0");
                    }

                    this._builder.FontTable.FontStyle = System.Drawing.FontStyle.Regular;

                    if (this._builder.FontTable.FontSize != this._builder.FontTable.DefaultFontSize)
                    {
                        this._builder.FontTable.FontSize = this._builder.FontTable.DefaultFontSize;
                        sb.AppendFormat("\\fs{0} ", this._builder.FontTable.DefaultFontSize);
                    }
                    if (this._builder.FontTable.Font != 0)
                    {
                        sb.Append("\\f0");
                        this._builder.FontTable.Font = 0;
                    }

                    if (this._builder.ColorTable.ForeColor != this._builder.ColorTable.DefaultForeColor)
                    {
                        this._builder.ColorTable.ForeColor = this._builder.ColorTable.DefaultForeColor;
                        sb.Append("\\cf0");
                    }
                    if (this._builder.ColorTable.BackColor != this._builder.ColorTable.DefaultBackColor)
                    {
                        this._builder.ColorTable.BackColor = this._builder.ColorTable.DefaultBackColor;
                        sb.Append("\\highlight0");
                    }
                    //if (_builder._alignment != StringAlignment.Near )
                    //{
                    //    _builder._alignment = StringAlignment.Near;
                    //    sb.Append("\\ql");
                    //}
                    if (sb.Length > len)
                    {
                        sb.Append(" ");
                    }
                }
                if (disposing)
                {
                    GC.SuppressFinalize(this);
                }
            }

            #endregion

            #region IDisposable Members

            public void Dispose()
            {
                this.Dispose(true);
            }

            #endregion
        }

        #endregion
    }
}


